package Test;

import enums.MSG_TYPE;
import sun.applet.resources.MsgAppletViewer;
import vo.Student;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 强大的stream java知识堂
 */
public class Test3 {

    static List<Student> list = Arrays.asList(new Student(1, "ff", "中国"),
            new Student(2, "bb", "美国"),
            new Student(3, "cc", "法国"),
            new Student(4, "dd", "日本"),
            new Student(5, "dd", "日本"));

    public static void test1() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        list.stream().skip(3).forEach(System.out::println);
    }

    //总数
    public static void test2() {
        Long collect = list.stream().collect(Collectors.counting());
        int size = list.size();
        System.out.println(size);
        System.out.println(collect);


    }

    //平均值
    public static void test3() {
        Double collect = list.stream().collect(Collectors.averagingDouble(Student::getId));
        System.out.println(collect);
    }

    //最大值
    public static void test4() {
        List<Student> list1=new ArrayList<>();
//        Student student = list.stream().max(Comparator.comparing(Student::getId)).get();
        Optional<Student> collect = list1.stream().collect(Collectors.maxBy((x, y) -> x.getId() - y.getId()));
        //System.out.println(collect.orElseGet(() ->new Student()).getId());
        System.out.println(collect.orElse(new Student()).getId());
    }

    //求和
    public static void test5() {
        Double collect = list.stream().collect(Collectors.summingDouble(Student::getId));
        System.out.println(collect);

    }

    //分组
    public static void test6(){
        Map<String, List<Student>> collect = list.stream().collect(Collectors.groupingBy(Student::getName));
        collect.forEach((k,v) ->{
            System.out.println(v);
        });
        System.out.println(collect);
    }

    //枚举匹配
    public static void test7(){
        boolean b = Arrays.stream(MSG_TYPE.values()).anyMatch(e -> e.getName() .equals("e") );
        System.out.println(b);

    }

    //id加对象组装成map
    public static void test8(){
        Map<Integer, Student> collect = list.stream().collect(Collectors.toMap(Student::getId, e -> e));
        System.out.println(collect);
    }





    public static void main(String[] args) {
        test4();
    }
}
